<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="160px" :rules="rules">
      <el-row style="text-align: center">
        <el-col :span="12">
          <el-form-item label="整车物料编码:" prop="materialDataId">
            <el-select v-model="formModel.materialDataId" clearable filterable>
              <el-option
                v-for="item in carMaterialDataOptions"
                :key="item.materialDataId"
                :label="item.materialNo+'-'+item.materialDesc"
                :value="item.materialDataId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆型号:" prop="clxh">
            <el-input v-model="formModel.clxh" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="一致性证书编码:" prop="consistencyCertificateId">
            <el-select v-model="formModel.consistencyCertificateId" clearable filterable>
              <el-option
                v-for="item in consistencyCertificateOptions"
                :key="item.consistencyCertificateId"
                :label="item.consistencyCertificateNo"
                :value="item.consistencyCertificateId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="环保信息公开编号:" prop="environmentCertificateId">
            <el-select v-model="formModel.environmentCertificateId" clearable filterable>
              <el-option
                v-for="item in environmentCertificateOptions"
                :key="item.environmentCertificateId"
                :label="item.environmentCertificateNo"
                :value="item.environmentCertificateId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="合格证编号:" prop="qualifiedCertificateId">
            <el-select v-model="formModel.qualifiedCertificateId" clearable filterable>
              <el-option
                v-for="item in qualifiedCertificateOptions"
                :key="item.qualifiedCertificateId"
                :label="item.qualifiedCertificateNo"
                :value="item.qualifiedCertificateId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="三包证书编号:" prop="threeGuaranteeId">
            <el-select v-model="formModel.threeGuaranteeId" clearable filterable>
              <el-option
                v-for="item in threeGuaranteeOptions"
                :key="item.threeGuaranteeId"
                :label="item.threeGuaranteeNo"
                :value="item.threeGuaranteeId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="能源消耗标识备案号:" prop="fuelId">
            <el-select v-model="formModel.fuelId" clearable filterable>
              <el-option
                v-for="item in fuelOptions"
                :key="item.fuelId"
                :label="item.bah"
                :value="item.fuelId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="一致性证书模板:" prop="consistencyCertificateModelFileName">
            <el-select v-model="formModel.consistencyCertificateModelFileName" clearable filterable>
              <el-option
                v-for="item in printModelOptions"
                :key="item.dataKey"
                :label="item.dataKey+'-'+item.dataValue"
                :value="item.dataKey"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="环保证书模板:" prop="environmentCertificateModelFileName">
            <el-select v-model="formModel.environmentCertificateModelFileName" clearable filterable>
              <el-option
                v-for="item in printModelOptions"
                :key="item.dataKey"
                :label="item.dataKey+'-'+item.dataValue"
                :value="item.dataKey"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="合格证书模板:" prop="qualifiedCertificateModelFileName">
            <el-select v-model="formModel.qualifiedCertificateModelFileName" clearable filterable>
              <el-option
                v-for="item in printModelOptions"
                :key="item.dataKey"
                :label="item.dataKey+'-'+item.dataValue"
                :value="item.dataKey"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="三包证书模板:" prop="threeGuaranteeModelFileName">
            <el-select v-model="formModel.threeGuaranteeModelFileName" clearable filterable>
              <el-option
                v-for="item in printModelOptions"
                :key="item.dataKey"
                :label="item.dataKey+'-'+item.dataValue"
                :value="item.dataKey"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="能源消耗标识模板:" prop="fuelModelFileName">
            <el-select v-model="formModel.fuelModelFileName" clearable filterable>
              <el-option
                v-for="item in printModelOptions"
                :key="item.dataKey"
                :label="item.dataKey+'-'+item.dataValue"
                :value="item.dataKey"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="配置序列号:" prop="pzxlh">
            <el-input v-model="formModel.pzxlh" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="轮胎规则:" prop="ltgg">
            <el-input v-model="formModel.ltgg" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆颜色:" prop="clys">
            <el-input v-model="formModel.clys" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="驱动形式:" prop="qdxs">
            <el-input v-model="formModel.qdxs" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="VIN码前八位:" prop="vinQbw">
            <el-input v-model="formModel.vinQbw" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="合格证备注:" prop="remarks">
            <el-input v-model="formModel.remarks" type="textarea" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态:" prop="status">
            <hey-select v-model="formModel.status" clcd="is_effective_cd" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="submit-area">
      <el-col :span="24">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>

export default {
  name: 'SupplierMnt',
  props: ['pageType', 'editData'],
  data() {
    return {
      printModelOptions: [],
      carMaterialDataOptions: [],
      consistencyCertificateOptions: [],
      environmentCertificateOptions: [],
      qualifiedCertificateOptions: [],
      threeGuaranteeOptions: [],
      fuelOptions: [],
      formModel: {
        certificateMaterialNoId: '',
        clxh: '',
        materialDataId: '',
        consistencyCertificateId: '',
        environmentCertificateId: '',
        qualifiedCertificateId: '',
        threeGuaranteeId: '',
        fuelId: '',
        consistencyCertificateModelFileName: '',
        environmentCertificateModelFileName: '',
        qualifiedCertificateModelFileName: '',
        threeGuaranteeModelFileName: '',
        fuelModelFileName: '',
        pzxlh: '',
        ltgg: '',
        clys: '',
        qdxs: '',
        vinQbw: '',
        remarks: '',
        status: '1'
      },
      // 老数据
      oldFormModel: {
      },
      rules: {}
    }
  },
  computed: {},
  mounted() {
    this.init()
    this.getPrintModelOptions()
    this.getCarMaterialDataOptions()
    this.getConsistencyCertificateList()
    this.getEnvironmentCertificateList()
    this.getQualifiedCertificateList()
    this.getThreeGuaranteeList()
    this.getFuelList()
  },
  methods: {
    init() {
      this.$form = this.$refs.form
      const { pageType, editData, formModel } = this
      if (pageType !== 'add') {
        this.assignData(editData, formModel)
        this.oldFormModel = JSON.parse(JSON.stringify(this.formModel))
      }
      this.initRules()
    },
    initRules() {
      const REQUIRED_MSG = this.CONST.REQUIRED_MSG
      this.rules = {
        certificate: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        materialDataId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        consistencyCertificateId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        qualifiedCertificateId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        vinQbw: this.HEY_VALIDATOR({ type: ['VINQBW'], required: true })
      }
      this.$nextTick(_ => {
        this.$form.clearValidate()
      })
    },
    // 获取打印模板列表
    getPrintModelOptions() {
      const param = { clcd: 'print_model_cd' }
      this.get('/dictionary/queryDataDictionary', param).then(res => {
        this.printModelOptions = res.list
      })
    },
    // 获取整车物料编号列表
    getCarMaterialDataOptions() {
      const param = { materialTypeKey: 'car', status: '1' }
      this.get('/materialData/listMaterialData', param).then(res => {
        this.carMaterialDataOptions = res
      })
    },
    // 获取一致性证书列表
    getConsistencyCertificateList() {
      const param = { status: '1' }
      this.get('/consistencyCertificate/listConsistencyCertificate', param).then(res => {
        this.consistencyCertificateOptions = res
      })
    },
    // 获取环保随车清单列表
    getEnvironmentCertificateList() {
      const param = { status: '1' }
      this.get('/environmentCertificate/listEnvironmentCertificate', param).then(res => {
        this.environmentCertificateOptions = res
      })
    },
    // 获取合格证列表
    getQualifiedCertificateList() {
      const param = { status: '1' }
      this.get('/qualifiedCertificate/listQualifiedCertificate', param).then(res => {
        this.qualifiedCertificateOptions = res
      })
    },
    // 获取三包证书列表
    getThreeGuaranteeList() {
      const param = { status: '1' }
      this.get('/threeGuarantee/listThreeGuarantee', param).then(res => {
        this.threeGuaranteeOptions = res
      })
    },
    // 获取能源消耗标识列表
    getFuelList() {
      const param = { status: '1' }
      this.get('/fuel/listFuel', param).then(res => {
        this.fuelOptions = res
      })
    },
    // 提交新增或修改
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.formModel.certificateMaterialNoId !== '') {
            // 判断是否修改
            if (JSON.stringify(this.formModel) !== JSON.stringify(this.oldFormModel)) {
              const tip = '修改成功！'
              const url = '/certificateMaterialNo/updateCertificateMaterialNo'
              this.post(url, this.formModel).then(res => {
                this.$info(tip, () => {
                  this.$emit('mntSuc', res)
                  this.closeDialog()
                })
              })
            } else {
              this.$emit('mntSuc')
              this.closeDialog()
            }
          } else {
            const url = '/certificateMaterialNo/saveCertificateMaterialNo'
            this.post(url, this.formModel).then(res => {
              const tip = '新增成功！'
              this.$info(tip, () => {
                this.$emit('mntSuc', res)
                this.closeDialog()
              })
            })
          }
        }
      })
    },
    closeDialog() {
      this.$parent.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
